const app = getApp();
const regeneratorRuntime = app.require('module/regenerator-runtime.js')
const comdataApi = app.require('api/comdataApi.js')
Component({
  //该主键为表单组件
  behaviors: ['wx://form-field'],
  //定义外部class名字
  externalClasses: ['checked-class', 'place-class'],
  /**
   * 组件的属性列表
   */
  properties: {
    initvalue: { // 初始值
      type: String,
      optionalTypes: [Array],
      value: ""
    },
    placeholder: { // 初始值
      type: String,
      value: "please fill out"
    },
    impose: { // 是否添加不限选项
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    value: null,
    valueIndex: [0, 0, 0], //默认选中第一个
    valuelabel: [null, null, null],
    areaList: [],
    areaListShow: [
      [],
      [],
      []
    ]
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      let that = this;
      setTimeout(() => {
        that.getAreaList();
      }, 100)

    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  observers: {
    //监听配置文件
    'initvalue': function (val) {
      console.log("触发了==", val);
      let that = this;
      let value = null;
      if (val) {
        if (Object.prototype.toString.call(val) == '[object Array]') {
          value = that.fttArray(val);
        } else {
          val = String(val);
          if (val != "") {
            value = val.split(",");
            value = that.fttArray(value);
          } else {
            value = that.fttArray([]);
          }
        }
      }
      setTimeout(() => {
        that.setValue(value);
      }, 200)
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    fttArray(arr = []) {
      if (arr.length == 3) {
        return arr;
      } else if (arr.length < 3) {
        while (arr.length < 3) {
          arr.push("");
        }
        return arr;
      } else if (arr.length > 3) {
        return [arr[0], arr[1], arr[2]];
      }
    },
    /**
     * 获取实时地区列表
     */
    async getAreaList() {
      let that = this;
      let areaList = this.data.areaList;
      if (areaList.length > 0) {
        return areaList;
      }
      let rs = await comdataApi.queryTreeList({
        impose: that.data.impose
      },{loading:"正在加载城市"});
      if (rs.code == 200) {
        areaList = rs.data;
        let areaListShow = [areaList, areaList[0].children, areaList[0].children[0].children];
        this.setData({
          areaList,
          areaListShow
        })
        console.log({
          areaList,
          areaListShow
        });
        return areaList;
      }
      return [];
    },
    /**
     * 将值进行初始化
     * @param {*} value 
     */
    async setValue(value) {
      let that = this;
      let valueIndex = [0, 0, 0];
      let valuelabel = [null, null, null];
      let areaList = await that.getAreaList();
      let areaListShow = [
        areaList,
        [],
        []
      ];
      if (value) {
        let templist = areaListShow[0];
        for (var i = 0; i < templist.length; i++) {
          if (templist[i].value == value[0]) {
            valueIndex[0] = i;
            valuelabel[0] = templist[i].label;
            areaListShow[1] = templist[i].children;
            break;
          }
        }

        templist = areaListShow[1];
        for (var i = 0; i < templist.length; i++) {
          if (templist[i].value == value[1]) {
            valueIndex[1] = i;
            valuelabel[1] = templist[i].label;
            areaListShow[2] = templist[i].children;
            break;
          }
        }

        templist = areaListShow[2];
        for (var i = 0; i < templist.length; i++) {
          if (templist[i].value == value[2]) {
            valueIndex[2] = i;
            valuelabel[2] = templist[i].label;
            break;
          }
        }
      } else {
        areaListShow = [areaList, areaList[0].children, areaList[0].children[0].children];
      }
      if(valuelabel[0]==null||valuelabel[1]==null||valuelabel[2]==null){
        value = null;
        valueIndex = [0,0,0];
        valuelabel = [null,null,null];
        areaListShow = [areaList, areaList[0].children, areaList[0].children[0].children];
      }

      //写入值
      this.setData({
        valueIndex,
        value,
        valuelabel,
        areaListShow
      })
    },
    bindMultiPickerChange(e) {
      console.log('选中了值', e.detail.value);
      let value = e.detail.value;
      let areaListShow = this.data.areaListShow;
      let setdata = {
        value: [areaListShow[0][value[0]].value,
          areaListShow[1][value[1]].value,
          areaListShow[2][value[2]].value
        ],
        valuelabel: [areaListShow[0][value[0]].label,
          areaListShow[1][value[1]].label,
          areaListShow[2][value[2]].label
        ]
      };
      this.setData(setdata)
      this.bindchange(setdata);
    },
    async bindMultiPickerColumnChange(e) {
      console.log('列发生改变：', e.detail)
      let column = e.detail.column;
      let value = e.detail.value;
      let areaList = await this.getAreaList();
      let areaListShow = this.data.areaListShow;
      let valueIndex = this.data.valueIndex;
      //改变了第一列
      if (column === 0 || column === "0") {
        areaListShow = [areaList, areaList[value].children, areaList[value].children[0].children];
        valueIndex = [value, 0, 0];
      } else if (column === 1 || column === "1") {
        //改变第二列
        areaListShow[2] = areaListShow[1][value].children;
        valueIndex = [valueIndex[0], value, 0];
      } else if (column === 2 || column === "2") {
        //改变第三列
        valueIndex = [valueIndex[0], valueIndex[1], value];
      }
      this.setData({
        areaListShow,
        valueIndex
      })
    },
    /**
     * 向外部提供change回调
     */
    bindchange: function (object) {
      this.triggerEvent('change', object);
    }
  }
})